package org.example.springbootpra1.demos.bean.enums.handler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.example.springbootpra1.demos.bean.enums.DeviceStatus;
import org.example.springbootpra1.demos.bean.enums.DeviceTypeEnum;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeviceTypeHandler extends BaseTypeHandler<DeviceTypeEnum> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, DeviceTypeEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getCode());
    }

    @Override
    public DeviceTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String code = rs.getString(columnName);
        if (code == null) {
            return null;
        }
        return DeviceTypeEnum.fromCode(code);
    }

    @Override
    public DeviceTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String code = rs.getString(columnIndex);
        if (code == null) {
            return null;
        }
        return DeviceTypeEnum.fromCode(code);
    }

    @Override
    public DeviceTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String code = cs.getString(columnIndex);
        if (code == null) {
            return null;
        }
        return DeviceTypeEnum.fromCode(code);
    }

}
